package com.fjec.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjec.common.Const;
import com.fjec.user.entity.SysUser;
import com.fjec.user.feign.MarketClient;
import com.fjec.user.service.*;
import org.apache.commons.collections.MapUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysRoleService sysRoleService;

    @Resource
    private ISysPermissionService sysPermissionService;

    @Resource
    private ISysUserLoginLogService sysUserLoginLogService;

    @Resource
    private MarketClient marketClient;

    @Override
    public List<String> getRoleCodesByOpenId(String openId) {
        return sysRoleService.getRoleCodesByOpenId(openId);
    }

    @Override
    public List<String> getRoleCodesByUserName(String userName) {
        return sysRoleService.getRoleCodesByUserName(userName);
    }

    @Override
    public List<String> getRoleCodesByUserId(Long userId) {
        return sysRoleService.getRoleCodesByUserId(userId);
    }

    @Override
    public SysUser getSysUserByUserName(String userName) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("UserName", userName)
                .or()
                .eq("Phone", userName);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        return sysUser;
    }

    @Override
    public List<String> getPermsByRoleCode(String roleCode) {
        return sysPermissionService.getPermsByRoleCode(roleCode);
    }

    @Override
    public void saveLoginLog(Long userId, Integer appType) {
        sysUserLoginLogService.saveLoginLog(userId, appType);
    }

    @Override
    public SysUser getSysUserByOpenId(String openId) {
        return sysUserService.getSysUserByUserAppId(openId, Const.APP_TYPE_WX_SMALL_APP);
    }

    @Override
    public Map<String, Object> getSessionInfo(Long userId, int busiType) {
        return marketClient.getSessionInfo(userId, busiType);
    }

    @Override
    public boolean existsPhone(String phone) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Phone", phone);
        Integer num = sysUserService.count(queryWrapper);
        return num > 0;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateUserInfoForCreateShop(Map<String, Object> dataMap) {
        Long userId = MapUtils.getLong(dataMap, "userId");
        String roleCode = MapUtils.getString(dataMap, "roleCode");
        String phone = MapUtils.getString(dataMap, "phone");
        // 保存用户角色
        sysRoleService.saveUserRole(userId, roleCode);
        // 更新用户电话号码
        SysUser entity = new SysUser();
        entity.setId(userId);
        entity.setPhone(phone);
        sysUserService.updateById(entity);
    }
}
